// 获取菜品数据
const { dishes, categories } = require('../../data/dishes');

Page({
  data: {
    dishes: [],
    categories: [],
    currentCategory: '全部',
    filteredDishes: [],
    selectedItems: []
  },
  
  onLoad: function () {
    this.setData({
      dishes,
      categories,
      filteredDishes: dishes
    });
    
    // 获取全局选中的菜品
    const app = getApp();
    if (app.globalData.selectedItems && app.globalData.selectedItems.length > 0) {
      this.setData({
        selectedItems: app.globalData.selectedItems
      });
    }
    
    this.filterDishes();
  },
  
  // 切换分类
  switchCategory: function(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      currentCategory: category
    });
    
    this.filterDishes();
  },
  
  // 过滤菜品
  filterDishes: function() {
    let filtered = [...this.data.dishes];
    const { currentCategory } = this.data;
    
    // 按分类过滤
    if (currentCategory !== '全部') {
      filtered = filtered.filter(dish => dish.category === currentCategory);
    }
    
    this.setData({
      filteredDishes: filtered
    });
  },
  
  // 检查菜品是否已选中
  isSelected: function(id) {
    return this.data.selectedItems.some(item => item.id === id);
  },
  
  // 切换选择菜品
  toggleSelectDish: function(e) {
    const dish = e.currentTarget.dataset.dish;
    const selectedItems = [...this.data.selectedItems];
    const maxItems = 8;
    
    // 检查是否已选中
    const index = selectedItems.findIndex(item => item.id === dish.id);
    
    if (index > -1) {
      // 如果已选中，则取消选中
      selectedItems.splice(index, 1);
    } else {
      // 如果未选中且未达到最大数量，则选中
      if (selectedItems.length < maxItems) {
        selectedItems.push(dish);
      } else {
        wx.showToast({
          title: `最多只能选择${maxItems}个菜品`,
          icon: 'none',
          duration: 1500
        });
        return;
      }
    }
    
    this.setData({
      selectedItems
    });
    
    // 更新全局数据
    getApp().globalData.selectedItems = selectedItems;
  },
  
  // 清空选择
  clearSelection: function() {
    this.setData({
      selectedItems: []
    });
    
    // 更新全局数据
    getApp().globalData.selectedItems = [];
  },
  
  // 随机选择
  randomSelect: function() {
    const { dishes, selectedItems } = this.data;
    const maxItems = 8;
    const currentCount = selectedItems.length;
    
    if (currentCount >= maxItems) {
      wx.showToast({
        title: `已达到最大选择数量`,
        icon: 'none',
        duration: 1500
      });
      return;
    }
    
    // 计算可以再选择的数量
    const remainingSlots = maxItems - currentCount;
    
    // 过滤掉已选择的菜品
    const availableDishes = dishes.filter(dish => 
      !selectedItems.some(item => item.id === dish.id)
    );
    
    if (availableDishes.length === 0) {
      wx.showToast({
        title: '没有更多可选菜品',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    
    // 随机选择菜品
    const shuffled = [...availableDishes].sort(() => 0.5 - Math.random());
    const randomCount = Math.min(remainingSlots, shuffled.length);
    const randomSelected = shuffled.slice(0, randomCount);
    
    this.setData({
      selectedItems: [...selectedItems, ...randomSelected]
    });
    
    // 更新全局数据
    getApp().globalData.selectedItems = [...selectedItems, ...randomSelected];
    
    wx.showToast({
      title: `已随机添加${randomCount}个菜品`,
      icon: 'success',
      duration: 1500
    });
  },
  
  // 接受轮盘结果
  onAcceptResult: function(e) {
    const dish = e.detail.dish;
    console.log('接受了轮盘结果:', dish);
  }
}) 